package cn.stylefeng.guns.sys.modular.syssite.service.impl;

import cn.stylefeng.guns.core.context.login.LoginContextHolder;
import cn.stylefeng.guns.sys.modular.org.param.SysOrgParam;
import cn.stylefeng.guns.sys.modular.syssite.entity.SysSiteAdmin;
import cn.stylefeng.guns.sys.modular.syssite.mapper.SysSiteAdminMapper;
import cn.stylefeng.guns.sys.modular.syssite.service.SysSiteAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SysSiteAdminServiceImpl extends ServiceImpl<SysSiteAdminMapper, SysSiteAdmin> implements SysSiteAdminService {

    @Override
    public void add(SysSiteAdmin siteAdmin) {
        Long tenantId = LoginContextHolder.me().getSysLoginUser().getTenantId();
        siteAdmin.setTenantId(tenantId);
        this.save(siteAdmin);
        if (siteAdmin.getChargeCodeList() != null && siteAdmin.getChargeCodeList().size() > 0) {
            this.baseMapper.addSiteCity(siteAdmin.getId(), siteAdmin.getChargeCodeList(),tenantId);
        }
    }

    @Override
    public boolean checkSiteAdd(SysSiteAdmin site, StringBuilder errorMsg) {
        Long tenantId = LoginContextHolder.me().getSysLoginUser().getTenantId();
            // 网点名称不可重复
            int count = this.baseMapper.countByName(null, site.getName(),tenantId);
            if (count > 0) {
                errorMsg.append("网点名称不可重复");
                return false;
            }
            // 服务城市不可重叠
            if (site.getChargeCodeList() != null && site.getChargeCodeList().size() > 0) {
                for (String code : site.getChargeCodeList()) {
                    int cityCode = this.baseMapper.countServiceCode(null, code,tenantId);
                    if (cityCode > 0) {
                        errorMsg.append("城市【"+this.baseMapper.getNameByCode(code)+"】已被其他网点绑定，不可重复");
                        return false;
                    }
                }
            }
            return true;

    }

    @Override
    public boolean checkSiteUpdate(SysSiteAdmin siteAdmin, StringBuilder errorMsg) {
        int count = this.baseMapper.countByName(siteAdmin.getId().toString(), siteAdmin.getName(), LoginContextHolder.me().getSysLoginUser().getTenantId());
        if (count > 0) {
            errorMsg.append("已存在相同名称网点");
            return false;
        }
        // 服务城市不可重叠
        if (siteAdmin.getChargeCodeList() != null && siteAdmin.getChargeCodeList().size() > 0) {
            for (String code : siteAdmin.getChargeCodeList()) {
                int cityCode = this.baseMapper.countServiceCode(siteAdmin.getId().toString(), code,LoginContextHolder.me().getSysLoginUser().getTenantId());
                if (cityCode > 0) {
                    errorMsg.append("城市【"+this.baseMapper.getNameByCode(code)+"】已被其他网点绑定，不可重复");
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public void updateSite(SysSiteAdmin siteAdmin) {
        this.baseMapper.updateById(siteAdmin);
        // 负责城市删除
        this.baseMapper.deleteCityCode(siteAdmin.getId());
        if (siteAdmin.getChargeCodeList() != null && siteAdmin.getChargeCodeList().size() > 0) {
            this.baseMapper.addSiteCity(siteAdmin.getId(), siteAdmin.getChargeCodeList(),LoginContextHolder.me().getSysLoginUser().getTenantId());
        }

    }

    @Override
    public void deleteSite(SysOrgParam sysOrgParam) {
        //获取网点id
        Long siteId = this.baseMapper.getSiteByOrgId(sysOrgParam.getId());
        if(siteId!=null){
            //删除网点id
            this.baseMapper.deleteById(siteId);
            // 负责城市删除
            this.baseMapper.deleteCityCode(siteId);
        }

    }


}
